import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from sys import argv


def send_mail(username_cn, username):
    sender = 'zdiai@zdiai.com'
    receivers = ['daidy@zdiai.com']  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱

    msgRoot = MIMEMultipart('related')
    msgRoot['From'] = Header("震兑<zdiai@zdiai.com>", 'utf-8')
    string_TO = "{}<{}@zdiai.com>".format(username_cn, username)
    msgRoot['To'] = Header(string_TO, 'utf-8')
    subject = '新员工入职小贴士'
    msgRoot['Subject'] = Header(subject, 'utf-8')

    msgAlternative = MIMEMultipart('alternative')
    msgRoot.attach(msgAlternative)

    mail_msg = """
    <html><body>
    <p>
        亲爱的 {}：
        您好！ 欢迎加入震兑工业智能科技有限公司！</br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;从现在起，您将与震兑智能共奋斗，共成长。公司将为您提供发展的机会、成长的空间，相信您在这里也能充分利用您的学识、工作经验及专业技能，更好的融入到这个大家庭。</br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;我们整理了一份新员工入职小贴士给到您，希望能为您解疑答惑，有任何问题可以和贴士中的联系人及时沟通哦~</br>
    
    </p>
    <p><img style="width:400px" src="cid:image1"></p>
    </body></html>
    """.format(username_cn)
    msgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8'))

    # 指定图片为当前目录
    fp = open('/Users/daidongyang/Pictures/zdiai/welcome-zdiai.png', 'rb')
    msgImage = MIMEImage(fp.read())
    fp.close()

    # 定义图片 ID，在 HTML 文本中引用
    msgImage.add_header('Content-ID', '<image1>')
    msgRoot.attach(msgImage)

    try:
        email_user = "zdiai@zdiai.com"
        email_pwd = ""
        mail_host = "smtp.exmail.qq.com"
        mail_port = 465

        smtp = smtplib.SMTP_SSL(mail_host)
        smtp.connect(mail_host, mail_port)
        smtp.login(email_user, email_pwd)
        smtp.sendmail(sender, receivers, msgRoot.as_string())
        smtp.quit()
        print("邮件发送成功")
    except smtplib.SMTPException:
        print("Error: 无法发送邮件")


def main():
    if len(argv) < 3:
        print("argv's len is less 2: ", argv)
        return
    username_cn = argv[1]
    username = argv[2]
    print("{}:  new user: {}<{}@zdiai.com>".format(argv[0], username_cn, username))
    send_mail(username_cn, username)


if __name__ == '__main__':
    main()
